package com.zhuxiang.test5;

import java.io.*;
import java.net.Socket;

/**
 * 客户端发送文件线程类
 */
public class ClientSendFileThread extends Thread {
    private Socket s;
    private String filePath;

    public ClientSendFileThread(Socket s, String filePath) {
        this.s = s;
        this.filePath = filePath;
    }

    @Override
    public void run(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        PrintStream ps = null;
        BufferedReader br = null;

        try {
            System.out.println(filePath+"发送中。。。");
            File file = new File(filePath);
            String fileName = file.getName();
            ps = new PrintStream(s.getOutputStream());
            ps.println(fileName);
            bis = new BufferedInputStream(new FileInputStream(filePath));

            bos = new BufferedOutputStream(s.getOutputStream());
            System.out.println("上传中。。。");
            byte[] bytes = new byte[1024*2];
            int len = 0;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
            bos.flush();
            System.out.println("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
